package cc.wanforme.st.server.base.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cc.wanforme.st.server.base.entity.Role;
import cc.wanforme.st.server.base.service.IRoleService;
import cc.wanforme.st.server.vo.ResMessage;
import cc.wanforme.st.server.vo.SearchQueryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanne
 * @since 2022-03-03
 */
@Controller
@RequestMapping("/base/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;
	
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('role:add')")
	@ResponseBody
	public ResMessage<Void> add(@RequestBody Role role) {
		roleService.saveVo(role);
		return ResMessage.success();
	} 
	
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('role:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody Role role) {
		roleService.updateVo(role);
		return ResMessage.success();
	}
	
	@PostMapping("/del/{roleCode}")
	@PreAuthorize("hasAuthority('role:delete')")
	@ResponseBody
	public ResMessage<Void> del(@PathVariable String roleCode ) {
		roleService.deleteVo(roleCode);
		return ResMessage.success();
	}
	
	/** 查询，可以根据 name 模糊查询
	 * @param queryVo
	 * @return
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('role:list')")
	@ResponseBody
	public ResMessage<PageInfo<Role>> list(@RequestBody SearchQueryVo<String> queryVo) {
		PageInfo<Role> result = roleService.searchRoles(queryVo.getPage(),
				queryVo.getSize(), queryVo.getParam());
		return ResMessage.success(result);
	}
	
}
